home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / REALITY / atom / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.0 KB  |  159 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* fileio.c
  18.  * --------
  19.  *
  20.  * $Revision: 1.13 $
  21.  */
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <values.h>
  27.  
  28. #include "mview.h"
  29. #include "geom.h"
  30.  
  31. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~ global variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  32.  
  33. FILE *infileptr;
  34.  
  35. /*
  36.  * readatoms()
  37.  */
  38. int readatoms(char *filename, atom_t **atoms, float *cx, float *cy, float *cz)
  39. {
  40.     int natoms=0, atomcount=0;
  41.     atom_t *aptr;
  42.     float tr=0.0, tg=0.0, tb=0.0;
  43.     float
  44.     maxx = MINFLOAT,
  45.     maxy = MINFLOAT,
  46.     maxz = MINFLOAT, 
  47.     minx = MAXFLOAT,
  48.     miny = MAXFLOAT,
  49.     minz = MAXFLOAT;
  50.  
  51.     if (!(infileptr = fopen(filename, "r"))) {
  52.     fprintf(stderr,"%s: cannot read atom file %s\n", ProgName, filename);
  53.     return 0;
  54.     /*DoExit(-1);*/
  55.     }
  56.     fscanf(infileptr,"natoms %d ", &natoms);
  57.  
  58.     aptr = *atoms = (atom_t *) malloc (natoms * sizeof(atom_t));
  59.  
  60.     while ((fscanf(infileptr," atom ") != -1) && (atomcount < natoms)) {
  61.     fscanf(infileptr," %f %f %f %f %f %f %f %d \n",
  62.             &(aptr->org_rad), &(aptr->x), &(aptr->y), &(aptr->z), 
  63.             &(aptr->r), &(aptr->g), &(aptr->b),
  64.             &(aptr->id));
  65.     aptr->rad = aptr->org_rad * RadScaleFactor;
  66.     /* update count for atom type */
  67.     AtomTab[aptr->id].natoms += 1;
  68.     /* update atom bounding box */
  69.     if (aptr->x < minx) minx = aptr->x;
  70.     if (aptr->y < miny) miny = aptr->y;
  71.     if (aptr->z < miny) minz = aptr->z;
  72.     if (aptr->x > maxx) maxx = aptr->x;
  73.     if (aptr->y > maxy) maxy = aptr->y;
  74.     if (aptr->z > maxy) maxz = aptr->z;
  75.     aptr++;
  76.     atomcount++;
  77.     }
  78.  
  79.     fclose(infileptr);
  80.  
  81.     /* set center of atom */
  82.     *cx = (minx + maxx) / 2;
  83.     *cy = (miny + maxy) / 2;
  84.     *cz = (minz + maxz) / 2;
  85.  
  86.     printf("total atoms: %d\n", atomcount);
  87.     return(atomcount);
  88. }
  89.  
  90. /*
  91.  * readbonds()
  92.  */
  93. int readbonds(char *filename, bond_t **bonds, float *cx, float *cy, float *cz)
  94. {
  95.     int nbonds=0, i=0;
  96.     bond_t *bptr;
  97.     float tr, tg, tb;
  98.     float
  99.     maxx = MINFLOAT,
  100.     maxy = MINFLOAT,
  101.     maxz = MINFLOAT, 
  102.     minx = MAXFLOAT,
  103.     miny = MAXFLOAT,
  104.     minz = MAXFLOAT;
  105.  
  106.     if (!(infileptr = fopen(filename, "r"))) {
  107.     fprintf(stderr,"%s: can't read bonds file %s\n", ProgName, filename);
  108.     return 0;
  109.     /*DoExit(-1);*/
  110.     }
  111.  
  112.     fscanf(infileptr,"nbonds %d ", &nbonds);
  113.  
  114.     bptr = *bonds = (bond_t *) malloc (2 * nbonds * sizeof(bond_t));
  115.  
  116.     while (fscanf(infileptr," line ") != -1) {
  117.     fscanf(infileptr," %f %f %f %f %f %f %f %f %f %d \n",
  118.             &(bptr->sx), &(bptr->sy), &(bptr->sz), 
  119.             &(bptr->ex), &(bptr->ey), &(bptr->ez), 
  120.             &(bptr->r), &(bptr->g), &(bptr->b),
  121.             &(bptr->id));
  122.     /* update count for bond type */
  123.     BondTab[bptr->id].nbonds += 1;
  124.     /* update atom bounding box */
  125.     if (bptr->sx < minx) minx = bptr->sx;
  126.     if (bptr->sy < miny) miny = bptr->sy;
  127.     if (bptr->sz < minz) minz = bptr->sz;
  128.     if (bptr->sx > maxx) maxx = bptr->sx;
  129.     if (bptr->sy > maxy) maxy = bptr->sy;
  130.     if (bptr->sz > maxz) maxz = bptr->sz;
  131.     if (bptr->ex < minx) minx = bptr->ex;
  132.     if (bptr->ey < miny) miny = bptr->ey;
  133.     if (bptr->ez < minz) minz = bptr->ez;
  134.     if (bptr->ex > maxx) maxx = bptr->ex;
  135.     if (bptr->ey > maxy) maxy = bptr->ey;
  136.     if (bptr->ez > maxz) maxz = bptr->ez;
  137.     bptr++;
  138.     i++;
  139.     if (i >= 2*nbonds) {
  140.         fprintf(stderr,"didn't malloc for enough bonds\n");
  141.         break;
  142.     }
  143.     }
  144.  
  145.     nbonds = i;
  146.  
  147.  
  148.     fclose(infileptr);
  149.  
  150.     /* set center of atom */
  151.     *cx = (minx + maxx) / 2;
  152.     *cy = (miny + maxy) / 2;
  153.     *cz = (minz + maxz) / 2;
  154.  
  155.     printf("total bonds: %d\n", nbonds);
  156.  
  157.     return (nbonds);
  158. }
  159.